package org.txazo.blog.commons.parse.base;

import java.lang.reflect.Constructor;

import org.txazo.blog.commons.io.base.Reader;

/**
 * ParserFactory
 * 
 * @author Tu XiaoZhou
 * @since 2013-08-30
 */
public class ParserFactory {

	private static final String CLASS_NAME_PACKAGES = "org.txazo.blog.commons.parse.base.";
	private static final String CLASS_NAME_PARSER = "Parser";

	public static Parser getParser(String codeType, Reader reader) {
		Parser parser = null;

		try {
			Class<?> targetClass = Class.forName(getFullClassName(codeType));
			Class<?>[] methodParameterTypes = new Class[] { Reader.class };
			Constructor<?> targetConstructor = targetClass
					.getConstructor(methodParameterTypes);
			Object[] params = new Object[] { reader };
			parser = (Parser) targetConstructor.newInstance(params);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return parser;
	}

	private static String getFullClassName(String codeType) {
		return CLASS_NAME_PACKAGES
				+ String.valueOf(codeType.charAt(0)).toUpperCase()
				+ codeType.substring(1, codeType.length()) + CLASS_NAME_PARSER;
	}

}
